Component({
  properties: {
    fixed: null,
  },
  data: {
    weekDays: [], //周期模板
    // selWeekDay: {},//当前选中的日期
    // currentMonth: 6,//当前月份
  },

  ready: function() {
    const that = this;

    that.setData({
      showMonth: that.data.currDate
    })
    that.init();
  },
  methods: {
    init: function() {
      const that = this;

      // 初始赋值
      let showMonth = that.time().typeB.split('-');

      that.setData({
        year: Number(showMonth[0]),
        month: Number(showMonth[1]),
        showMonth: showMonth[0] + '-' + showMonth[1] + '-01',
        hiddenRight: (Number(showMonth[0]) === that.time(0, true).year && Number(showMonth[1]) === Number(that.time(0, true).month)),
      })

      that.calendarModel();
    },
    /**
     * 日历模板
     */
    calendarModel: function() {
      const that = this;
      const currDate = that.data.currDate;
      const month = that.data.month || 0;
      let selWeekDay = that.data.selWeekDay;
      let tempArr = [];
      // 1号前空白格
      for (let i = 7; i > 0; i--) {
        let tempObj = {};
        // 2018.5.14修改（wx:key值不能为空）
        tempObj.time = that.time(-i).typeB;
        tempObj.day = that.time(-i).day;
        tempObj.year = that.time(-i).year;
        tempObj.month = Number(that.time(-i).month);
        tempObj.week = that.time(-i).week;
        tempObj.weekText = that.time(-i).weekText;
        tempObj.status = 0;
        // 当前月的第几周，周一开始
        if (tempArr.length == 0) {
          tempObj.weekNum = 1;
        } else {
          let weekNum = tempArr[tempArr.length - 1].weekNum || 0;
          if (tempObj.week == 1) weekNum++;
          tempObj.weekNum = weekNum || 1;
        }
        if ((tempArr == 0 && tempObj.week == 1) || tempArr.length > 0) tempArr.push(tempObj);
      }

      // 日历
      for (let i = 0; i < 40; i++) {
        let tempObj = {};
        tempObj.time = that.time(i).typeB;
        tempObj.day = that.time(i).day;
        tempObj.year = that.time(i).year;
        tempObj.month = Number(that.time(i).month);
        tempObj.week = that.time(i).week;
        tempObj.weekText = that.time(i).weekText;
        // 每日24点毫秒数是否大于当前毫秒数
        tempObj.status = (that.time(i).msec + that.time().dayMsec) > that.time(0, true).msec ? 1 : 0;
        if (currDate == tempObj.time) {
          tempObj.flag = 1;
          that.setData({
            selTempYear: that.time(i).year,
            selTempMonth: Number(that.time(i).month),
            selTempDay: that.time(i).day,
            selTempWeekText: that.time(i).weekText,
          })
        }
        // 当前月的第几周，周一开始
        let weekNum = tempArr[tempArr.length - 1].weekNum || 0;
        if (tempObj.week == 1) weekNum++;
        tempObj.weekNum = weekNum || 1;
        if (!selWeekDay && tempObj.time == that.time(0, true).typeB) {
          selWeekDay = tempObj;
          that.triggerEvent('selectDay', selWeekDay);
        }
        tempArr.push(tempObj);
        if (tempObj.month != month && tempObj.week == 0) break;
      }

      console.log(tempArr)
      that.setData({
        selWeekDay,
        infoData: tempArr,
      })
    },
    /**
     * 选择时间
     */
    changeTime: function(event) {
      var that = this;
      var index = event.currentTarget.dataset.index;
      let infoData = that.data.infoData;
      let selWeekDay = infoData[index];

      that.triggerEvent('selectDay', selWeekDay);

      that.setData({
        selWeekDay: selWeekDay,
        openCalendar:false,
      })
    },
    /**
     * 日历折叠、打开切换
     */
    changeOpen:function(){
      const that = this;
      const openCalendar = that.data.openCalendar;
      that.setData({
        openCalendar:!openCalendar
      })
    },
    /**
     * 获取年月日
     * params n 与当天时间的差值
     */
    time: function(n = 0, isCurrent) {
      const that = this;
      let showMonth = that.data.showMonth;
      let date;

      if (!showMonth || isCurrent) {
        date = new Date();
      } else {
        date = new Date(showMonth);
      }

      date.setDate(date.getDate() + n);
      let year = date.getFullYear();
      let month = date.getMonth() + 1;
      let day = date.getDate();
      let week = date.getDay();
      let msec = date.getTime();
      if (month < 10) {
        month = '0' + month;
      }

      let weekText;
      switch (week) {
        case 1:
          weekText = "一";
          break;
        case 2:
          weekText = "二";
          break;
        case 3:
          weekText = "三";
          break;
        case 4:
          weekText = "四";
          break;
        case 5:
          weekText = "五";
          break;
        case 6:
          weekText = "六";
          break;
        case 0:
          weekText = "日";
          break;
      }

      return {
        typeA: month + '月' + day + '日',
        typeB: year + '-' + month + '-' + (day < 10 ? '0' + day : day),
        year: year,
        month: month,
        day: day,
        week: week,
        weekText: weekText,
        msec: msec, //毫秒
        dayMsec: 1000 * 60 * 60 * 24, //一天的毫秒
      };
    },
  },
})